import { createRouter, createWebHistory } from 'vue-router'
import { isAdminLoggedIn } from '../utils/auth'
import { createAsyncComponent } from '../utils/asyncComponent'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'home',
            component: createAsyncComponent(() => import('../views/Home.vue'), '首页加载中...')
        },
        {
            path: '/blog',
            name: 'blog',
            component: createAsyncComponent(() => import('../views/Blog.vue'), '博客列表加载中...')
        },
        {
            path: '/blog-detail/:id',
            name: 'blog-detail',
            component: createAsyncComponent(() => import('../views/BlogDetail.vue'), '文章详情加载中...')
        },
        {
            path: '/timeline',
            name: 'timeline',
            component: createAsyncComponent(() => import('../views/Timeline.vue'), '时间线加载中...')
        },
        {
            path: '/guestbook',
            name: 'guestbook',
            component: createAsyncComponent(() => import('../views/Guestbook.vue'), '留言板加载中...')
        },
        {
            path: '/about',
            name: 'about',
            component: createAsyncComponent(() => import('../views/About.vue'), '关于我加载中...')
        },
        {
            path: '/friends',
            name: 'friends',
            component: createAsyncComponent(() => import('../views/Friends.vue'), '友链加载中...')
        },
        {
            path: '/contact',
            name: 'contact',
            component: createAsyncComponent(() => import('../views/Contact.vue'), '联系页面加载中...')
        },
        {
            path: '/subscribe',
            name: 'subscribe',
            component: createAsyncComponent(() => import('../views/Subscribe.vue'), '订阅页面加载中...')
        },
        {
            path: '/category/:category',
            name: 'category',
            component: createAsyncComponent(() => import('../views/Blog.vue'), '分类页面加载中...'),
            props: true
        },
        {
            path: '/category/:category/:subcategory',
            name: 'subcategory',
            component: createAsyncComponent(() => import('../views/Blog.vue'), '子分类页面加载中...'),
            props: true
        },
        {
            path: '/tag/:tag',
            name: 'tag',
            component: createAsyncComponent(() => import('../views/Blog.vue'), '标签页面加载中...'),
            props: true
        },
        {
            path: '/login',
            name: 'login',
            component: createAsyncComponent(() => import('../views/Login.vue'), '登录页面加载中...')
        },
        {
            path: '/register',
            name: 'register',
            component: createAsyncComponent(() => import('../views/Register.vue'), '注册页面加载中...')
        },
        // 错误边界测试页面
        {
            path: '/test-error',
            name: 'test-error',
            component: createAsyncComponent(() => import('../test/TestError.vue'), '错误测试页面加载中...')
        },
        // 错误页面测试页面
        {
            path: '/test-error-pages',
            name: 'test-error-pages',
            component: createAsyncComponent(() => import('../test/TestErrorPages.vue'), '错误页面测试加载中...')
        },
        // 记住我功能测试页面
        {
            path: '/test-remember-me',
            name: 'test-remember-me',
            component: createAsyncComponent(() => import('../test/TestRememberMe.vue'), '记住我功能测试加载中...')
        },
        // 安全防护测试页面
        {
            path: '/test-security',
            name: 'test-security',
            component: createAsyncComponent(() => import('../test/TestSecurity.vue'), '安全防护测试页面加载中...')
        },
        // 请求拦截器测试页面
        {
            path: '/test-http-interceptor',
            name: 'test-http-interceptor',
            component: createAsyncComponent(() => import('../test/HttpInterceptorDemo.vue'), '请求拦截器测试页面加载中...')
        },
        // 管理后台登录页面
        {
            path: '/admin/login',
            name: 'admin-login',
            component: createAsyncComponent(() => import('../views/admin/AdminLogin.vue'), '管理后台登录加载中...')
        },
        // 管理后台路由
        {
            path: '/admin',
            name: 'admin',
            component: createAsyncComponent(() => import('../components/AdminLayout_admin.vue'), '管理后台加载中...'),
            // component: () => import('../components/AdminLayout_admin.vue'),
            redirect: '/admin/dashboard',
            children: [
                {
                    path: 'dashboard',
                    name: 'admin-dashboard',
                    component: createAsyncComponent(() => import('../views/admin/Dashboard.vue'), '仪表盘加载中...')
                },
                {
                    path: 'posts',
                    name: 'admin-posts',
                    component: createAsyncComponent(() => import('../views/admin/PostList.vue'), '文章管理加载中...')
                },
                {
                    path: 'posts/edit',
                    name: 'admin-post-edit',
                    component: createAsyncComponent(() => import('../views/admin/PostEdit.vue'), '文章编辑加载中...')
                },
                {
                    path: 'posts/edit/:id',
                    name: 'admin-post-edit-id',
                    component: createAsyncComponent(() => import('../views/admin/PostEdit.vue'), '文章编辑加载中...')
                },
                {
                    path: 'categories',
                    name: 'admin-categories',
                    component: createAsyncComponent(() => import('../views/admin/CategoryList.vue'), '分类管理加载中...')
                },
                {
                    path: 'tags',
                    name: 'admin-tags',
                    component: createAsyncComponent(() => import('../views/admin/TagList.vue'), '标签管理加载中...')
                },
                {
                    path: 'friends',
                    name: 'admin-friends',
                    component: createAsyncComponent(() => import('../views/admin/FriendList.vue'), '友链管理加载中...')
                },
                {
                    path: 'comments',
                    name: 'admin-comments',
                    component: createAsyncComponent(() => import('../views/admin/CommentList.vue'), '评论管理加载中...')
                },
                {
                    path: 'cache',
                    name: 'admin-cache',
                    component: createAsyncComponent(() => import('../views/admin/CacheManagement.vue'), '缓存管理加载中...')
                },
                {
                    path: 'settings',
                    name: 'admin-settings',
                    component: createAsyncComponent(() => import('../views/admin/SystemSettings.vue'), '系统设置加载中...')
                }
            ]
        },
        // 错误页面路由
        {
            path: '/404',
            name: 'not-found',
            component: createAsyncComponent(() => import('../components/errors/NotFound.vue'), '404页面加载中...')
        },
        {
            path: '/500',
            name: 'server-error',
            component: createAsyncComponent(() => import('../components/errors/ServerError.vue'), '500页面加载中...')
        },
        {
            path: '/403',
            name: 'forbidden',
            component: createAsyncComponent(() => import('../components/errors/Forbidden.vue'), '403页面加载中...')
        },
        {
            path: '/error/:code',
            name: 'general-error',
            component: createAsyncComponent(() => import('../components/errors/GeneralError.vue'), '错误页面加载中...'),
            props: true
        },
        // 404 通配符路由 - 必须放在最后
        {
            path: '/:pathMatch(.*)*',
            name: 'not-found-catch',
            redirect: '/404'
        }
    ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
    // 检查是否访问管理后台页面
    if (to.path.startsWith('/admin') && to.path !== '/admin/login') {
        if (!isAdminLoggedIn()) {
            // 未登录，重定向到管理后台登录页面
            next('/admin/login')
            return
        }
    }

    // 如果已登录且访问登录页面，重定向到管理后台首页
    if (to.path === '/admin/login' && isAdminLoggedIn()) {
        next({ name: 'admin-dashboard' })
        return
    }

    next()
})

export default router 